<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Holiday</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link href="./EldercareMasterPage/_finalstyle.css" rel="stylesheet" />
	<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
	<link rel="stylesheet" type="text/css" href="./jquery/easyui.css">
	<link rel="stylesheet" type="text/css" href="./jquery/icon.css">
</head>
<?php
	define('CONFIG_FILE',true);
	include './config.php';
	define('DB_FILE',true);

	session_start();
	if (empty($_SESSION["username"])||empty($_SESSION["userType"])) {
		Header("Location: ./Index.php");
		exit();
	}else{
		$userType = $_SESSION["userType"];
	}
	
	if($userType!="System Admin"){
		$url="./Index.php";
		include_once './Tool/Common/Redirect.php';
		exit();
	}
	// set timeout period in seconds
	$inactive = 7200;
	// check to see if $_SESSION['timeout'] is set
	if (isset($_SESSION["timeout"])) {
		$session_life = time() - $_SESSION["timeout"];
		if ($session_life > $inactive) {
			session_destroy();
			Header("Location: Timeout.php");
		}
	}
	$_SESSION["timeout"] = time();
?>
<body>
	<div class="banner">
	</div>
	<div id="main">
		<div class="left-wrapper">
			<div class="logo">
				<img src="images/eldercare.gif" alt="logo" width="240px" />
			</div>
			<div class="menu-wrapper">
				<div id="cssmenu">
					<?php
						include_once('./Sidebar/_SidebarSA.php');	
					?>
				</div>
			</div>
		</div>      
		<div id="content">
			<div class="content2">
				<b style="float:right; margin: 0px 0 0 0">  
					<?php
						if (!empty($_SESSION["centreName"])) {
							$Username = $_SESSION["username"];
							$centreName = $_SESSION["centreName"];
							echo "Welcome, " . $Username . " ! " . $centreName . " Branch";
						} else {
							$Username = $_SESSION["username"];
							echo "Welcome, " . $Username;
						}
					?>
					<input type="submit" class="btn btn-danger" value="Logout" onclick="parent.location = 'logout.php'">
					</b>
					</p>
					<br>
				<div style="float:center;">
					<br>
					<h3>Holidays</h3>
					<br>
					<table id="dg" title="Holidays" class="easyui-datagrid" style="width:980px;height:340px; "
						   url="CRUDHols/get_hols.php"
						   toolbar="#toolbar" pagination="true"
						   rownumbers="true" fitColumns="true" singleSelect="true">
						<thead>
							<tr>
								<th field="HolidayName" width="50" sortable="true">Holiday Name</th>
								<th field="Date" width="50" sortable="true">Date</th>
						<tbody style ="display:none">
						<th field="ID" width="50">ID</th></tbody>
						</tr>
						</thead>
					</table>
					<div id="toolbar">
						<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editHol()">Edit Holiday</a>
					</div>

					<div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
						 closed="true" buttons="#dlg-buttons">
						<div class="ftitle">Holiday Information</div>
						<form id="fm" method="post" novalidate>
							<div class="fitem">
								<label>Holiday Name: </label>
								<input id="HolidayName" name="HolidayName" class="easyui-validatebox" required="true" style="width:130px" height='auto'>
							</div>
							<div class="fitem">
								<label>Date: </label>
								<input id="Date" name="Date" class="easyui-datebox"></input>
							</div>
							<div class="fitem">
								<input id="ID" name="ID" class="easyui-validatebox" type="hidden" required="true" style="width:155" height='auto'>
							</div>

						</form>
					</div>
					<div id="dlg-buttons">
						<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="updateHol()">Save</a>
						<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">Cancel</a>
					</div>
				</div><p>
			</div>
			<div><span id="result"></span></div>
			<div class="clear"></div>
		</div>
	</div>
	<div id="footer">&copy; 2013 MP22 IIT-IT Temasek Polytechnic</div>
</body>
<script type="text/javascript" src="./jquery/jquery-1.6.min.js"></script>
<script type="text/javascript" src="./jquery/jquery.easyui.min.js"></script>
<script type="text/javascript">
	$('#Date').datebox({
	    value: (new Date().toString('dd-MMM-yyyy')), 
	    formatter : function(date){
	        var y = date.getFullYear();
			var m = date.getMonth()+1;
			var d = date.getDate();
			return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
	    },
	    parser : function(s){
	        var t = Date.parse(s);
	        if (!isNaN(t)){
	            return new Date(t);
	        } else {
	            return null;
	        }
	    }
	});

	var url;
	function newHol() {
		$('#dlg').dialog('open').dialog('setTitle', 'New Holiday');
		$('#fm').form('clear');
	}
	function editHol() {
		var row = $('#dg').datagrid('getSelected');
		if (row) {
			$('#dlg').dialog('open').dialog('setTitle', 'Edit Holiday');
			$('#fm').form('load', row);
		}
	}
	function updateHol() {
		$('#fm').form('submit', {
			onSubmit: function() {
				$(this).form('validate');
				//validate input
				var flagHolidayName=0;	
				var flagUpdateDate=0;
				var flagID=0;
				$.ajax({
					async : false,
					type : "POST",
					url : "./CRUDHols/validateUpdate.php", //the page/method you wanna validate
					data : {
						HolidayName:$("#HolidayName").val(),
						UpdateDate: $('[name="Date"]').val(),
						ID: $("#ID").val(),
					},
					success : function(data) {
						var data = JSON.parse(data);
						flagHolidayName=data.flagHolidayName;
						flagUpdateDate=data.flagUpdateDate;
						flagID=data.flagID;
					}
				});
				if(flagHolidayName==0){
					$.messager.show({
						title: 'Error',
						msg: 'Invalid Holiday Name. Holiday Name cannot contain any special characters except brackets and single quatation marks.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else if(flagUpdateDate==0){
					$.messager.show({
						title: 'Error',
						msg: 'Invalid Date Format.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else if(flagID==0){
					$.messager.show({
						title: 'Error',
						msg: 'Invalid Holiday ID. Holiday ID cannot contain any letters and special characters.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else{
					return $(this).form('validate');	
				}
			},
			url: './CRUDHols/update_hols.php',
			success: function(result) {
				var result = eval('(' + result + ')');
				if (result.success) {
					$('#dlg').dialog('close');		// close the dialog
					$('#dg').datagrid('reload');	// reload the user data
				} else {
					$.messager.show({
						title: 'Error',
						msg: result.msg,
						showType:'fade',
						style:{
							right:'',
							bottom:''
						}
					});
				}
			}
		});
	}

	$(function(){ 
        $("#Date").next().find("input.validatebox-text").attr("readonly","readonly");  
    });
</script>	
</html>